/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.mobile.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.cmcc.zysoft.infocollection.mobile.dao.MServiceItemDao;
import com.cmcc.zysoft.infocollection.model.Industry;
import com.cmcc.zysoft.infocollection.model.ServiceItem;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: MServiceItemService.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-16 上午11:38:12
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Service
public class MServiceItemService extends BaseServiceImpl<ServiceItem, String> {

	@Resource
	private MServiceItemDao mServiceItemDao;
	
	/**
	 * 属性名称: mResourcePropertyService.
	 * 类       型: MResourcePropertyService.
	 */
	@Resource
	private MResourcePropertyService mResourcePropertyService;
	
	@Override
	public HibernateBaseDao<ServiceItem, String> getHibernateBaseDao() {
		return this.mServiceItemDao;
	}
	
	/**
	 * 根据服务项目ID判断其该服务项目是否存在.
	 * @param serviceItemId 服务项目ID
	 * @return Boolean(true-存在,false-不存在)
	 */
	public Boolean isServiceItemExist(String serviceItemId) {
		List<Map<String, Object>> list = this.mServiceItemDao.isServiceItemExist(serviceItemId);
		if(null != list && !list.isEmpty()) {
			return true;
		}
		return false;
	}
	
	/**
	 * 判断指定行业下是否存在必填字段为空(true-存在,false-不存在).
	 * @param map
	 * @param nodeName 节点名称
	 * @param nodeValue 节点值
	 * @return Boolean
	 */
	public Boolean isNodeBlank(Map<String, Object> map, String nodeName, String nodeValue) {
		Boolean result = false;
		//节点值为空,判断该行业下相应的资源属性是否必填(0-必填,1-非必填)
		if(!StringUtils.hasText(nodeValue)) {
			switch (nodeName) {
				case "seqnum"         : break;
				case "id"             : break;
				case "province_name"  : break;
				case "city_name"      : break;
				case "district_name"  : break;
				case "trade_name"     : break;
				case "name"           : 
					if("0".equals(map.get("caterName").toString())) {
						result = true;
					}
					break;
				case "branch_name"    : 
					if("0".equals(map.get("caterStore").toString())) {
						result = true;
					}
					break;
				case "short_name"     : 
					if("0".equals(map.get("shortName").toString())) {
						result = true;
					}
					break;
				case "telno"          : 
					if("0".equals(map.get("caterMobile").toString())) {
						result = true;
					}
					break;
				case "address"        : 
					if("0".equals(map.get("caterDetailAdd").toString())) {
						result = true;
					}
					break;
				case "region"         : 
					if("0".equals(map.get("caterBusi").toString())) {
						result = true;
					}
					break;
				case "landmark"       : 
					if("0".equals(map.get("caterBuild").toString())) {
						result = true;
					}
					break;
				case "coupon_source"  : break;
				case "charge_type"    : 
					if("0".equals(map.get("caterSwingCard").toString())) {
						result = true;
					}
					break;
				case "business_hours" : 
					if("0".equals(map.get("caterTime").toString())) {
						result = true;
					}
					break;
				case "bus_route"      : 
					if("0".equals(map.get("caterTransit").toString())) {
						result = true;
					}
					break;
				case "feature"        : 
					if("0".equals(map.get("caterAtmo").toString())) {
						result = true;
					}
					break;
				case "recommendation" : 
					if("0".equals(map.get("caterReco").toString())) {
						result = true;
					}
					break;
				case "introduction"   : 
					if("0".equals(map.get("caterMerIntr").toString())) {
						result = true;
					}
					break;
				case "reservation"    : 
					if("0".equals(map.get("caterAccReser").toString())) {
						result = true;
					}
					break;
				case "style"          : break;
				case "special_offer"  : 
					if("0".equals(map.get("caterDish").toString())) {
						result = true;
					}
					break;
				case "cost"           : 
					if("0".equals(map.get("caterLevel").toString())) {
						result = true;
					}
					break;
				case "private_room"   : 
					if("0".equals(map.get("caterRoom").toString())) {
						result = true;
					}
					break;
				case "parking"        : 
					if("0".equals(map.get("caterPark").toString())) {
						result = true;
					}
					break;
				case "city_id"        : break;
				case "trade_id"       : 
					if("0".equals(map.get("caterServ").toString())) {
						result = true;
					}
					break;
				case "province_id"    : break;
				case "district_id"    : 
					if("0".equals(map.get("caterReg").toString())) {
						result = true;
					}
					break;
				case "ticket_price"   : break;
				case "x"              : 
					if("0".equals(map.get("x").toString())) {
						result = true;
					}
					break;
				case "y"              : 
					if("0".equals(map.get("y").toString())) {
						result = true;
					}
					break;
				case "is_exist"       : break;
				case "bizcode"        : break;
				case "distance"       : break;
				case "wl_discount"    : break;
				case "updated_at"     : break;
				default               : break;
			}
		}
		return result;
	}
	
	/**
	 * 判断是否过期(true-存在,false-不存在).
	 * @param tradeId 服务项目ID
	 * @param updateDate 更新时间
	 * @return Boolean
	 * @throws Exception
	 */
	public Boolean isExpired(String tradeId, String updateDate) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date nowDate = new Date();
		Date date = sdf.parse(updateDate);
		ServiceItem serviceItem = this.mServiceItemDao.get(tradeId);
		Industry industry = serviceItem.getIndustry();
		String industryLife = industry.getIndustryLife().toString();
		Double life = Double.parseDouble(industryLife);
		Double result = (double) ((nowDate.getTime() - date.getTime()) / (30*24*60));
		if(result > (life *60 * 1000)) {
			return true;
		}
		return false;
	}
}